//
//  SaveTools.m
//  UI_15_Tools_Reachability
//
//  Created by dllo on 15/8/11.
//  Copyright (c) 2015年 liuchunling. All rights reserved.
//

#import "SaveTools.h"

@implementation SaveTools

// 工具类无初始化方法
+ (NSString *)pathWithName:(NSInteger)name directory:(NSSearchPathDirectory)directory
{
    NSString *local = [[NSSearchPathForDirectoriesInDomains(directory, NSUserDomainMask, YES) firstObject] stringByAppendingPathComponent:@"localPath"];
    NSFileManager *fileManage = [NSFileManager defaultManager];
    [fileManage createDirectoryAtPath:local withIntermediateDirectories:NO attributes:nil error:nil];
    NSString *path = [NSString stringWithFormat:@"%@/%lu", local, name];
    NSLog(@"localPath : %@", path);
    return path;
}

// 存储NSData
+ (BOOL)saveDataWithName:(NSInteger)name directory:(NSSearchPathDirectory)directory data:(NSData *)data
{
    NSString *path = [SaveTools pathWithName:name directory:directory];
    
    return [data writeToFile:path atomically:YES];
}

// 读取NSData
+ (NSData *)readDataWithName:(NSInteger)name directory:(NSSearchPathDirectory)directory
{
    NSString *path = [SaveTools pathWithName:name directory:directory];
    
    return [NSData dataWithContentsOfFile:path];
}

// 存储NSString
+ (BOOL)saveStrWithName:(NSInteger)name directory:(NSSearchPathDirectory)directory str:(NSString *)str
{
    NSString *path = [SaveTools pathWithName:name directory:directory];
    
    return [str writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:nil];
}

// 读取NSString
+ (NSString *)readStrWithName:(NSInteger)name directory:(NSSearchPathDirectory)directory
{
    NSString *path = [SaveTools pathWithName:name directory:directory];
    
    return [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
}

// 存储NSDictionary
+ (BOOL)saveDicWithName:(NSInteger)name directory:(NSSearchPathDirectory)directory dic:(NSDictionary *)dic
{
    NSString *path = [SaveTools pathWithName:name directory:directory];
    
    return [dic writeToFile:path atomically:YES];
}

// 读取NSDictionary
+ (NSDictionary *)readDicWithName:(NSInteger)name directory:(NSSearchPathDirectory)directory
{
   NSString *path = [SaveTools pathWithName:name directory:directory];
    
    return [NSDictionary dictionaryWithContentsOfFile:path];
}

// 存储NSArray
+ (BOOL)saveArrWithName:(NSInteger)name directory:(NSSearchPathDirectory)directory arr:(NSArray *)arr
{
   NSString *path = [SaveTools pathWithName:name directory:directory];
    
    return [arr writeToFile:path atomically:YES];
}

// 读取NSArray
+ (NSArray *)readArrWithName:(NSInteger)name directory:(NSSearchPathDirectory)directory
{
   NSString *path = [SaveTools pathWithName:name directory:directory];
    NSLog(@"======%lu", name);
    return [NSArray arrayWithContentsOfFile:path];
}

@end
